#include <types.h>

#define SECTOR_SIZE 512
/**
 * @brief part_desc
 * 描述一个moos文件系统的分区信息
 * 
 * 
 * 
 * fat->unit_cnt = small->unit_cnt + medium->unit_cnt + large->unit_cnt;
 * inode->unit_cnt = fat->unit_cnt
 * 
 * 
 */
struct part_desc {
    uint32_t first_blk;                 /*   分区起始块的位置   */
    uint32_t unit_size;                 /*   在这个分区，一次的分配单元   */
    uint32_t unit_cnt;                  /*   单元总数   */
    uint32_t unit_free;                 /*   空闲单元数量   */
};

/**
 * @brief 超级块结构体
 * 
 * 描述一个moos文件系统基本信息
 * 如：fat、inode、small、medium、large分区所在的位置
 *    一个块的大小
 */
struct moos_super_block {

    struct part_desc fat;
    struct part_desc inode;

    struct part_desc small;             /*   unit_size =  1    4K   */
    struct part_desc medium;            /*   unit_size =  512  2M  */
    struct part_desc large;             /*   unit_size =  1024 4M  */

    float m_prop, l_prop;               /*   m 和 large区的占比，底数为10   */
    uint32_t blk_cnt;
    uint32_t blk_size;
    uint32_t magic;
};

/**
 * @brief moos_fill_super
 * 底数为10
 * @param super 
 * @param m_prop 
 * @param l_prop 
 */
#define MOOS_FIRST_BLK 1
#define MOOS_MAGIC 0xaabbccddee
#define blk_to_unit(blk, size)  ((blk) / (size))
#define blk_to_sec(blk, size)     ((blk) * (size))
void m_fill_data(struct moos_super_block* super)
{

    uint32_t blk_cnt = super->blk_cnt;
    uint32_t m_blk_cnt = (super->m_prop / 10) * blk_cnt,
             l_blk_cnt = (super->l_prop / 10) * blk_cnt,
             s_blk_cnt = blk_cnt - m_blk_cnt - l_blk_cnt;

    struct part_desc *s = &super->small, 
                     *m = &super->medium,
                     *l= &super->large;

    s->unit_free = s->unit_cnt = blk_to_unit(s_blk_cnt, s->unit_size);
    m->unit_free = m->unit_cnt = blk_to_unit(m_blk_cnt, m->unit_size);
    l->unit_free = l->unit_cnt = blk_to_unit(l_blk_cnt, l->unit_size);

    l->first_blk = blk_cnt - l->unit_cnt * l->unit_size + MOOS_FIRST_BLK;
    m->first_blk = l->first_blk - m->unit_cnt * m->unit_size;
    s->first_blk = MOOS_FIRST_BLK;
}

void m_fill_misc(struct moos_super_block* super)
{

}

void m_init_super(struct moos_super_block* super, float m_prop, float l_prop)
{
    super->blk_cnt = ;
    super->blk_size = ;
    super->magic = MOOS_MAGIC;

    super->l_prop = l_prop;
    super->m_prop = m_prop;

}

void prt_super(struct moos_super_block* super)
{
    void prt_part(struct part_desc *p)
    {
        static int i=0;
        char *name[] = {
            "Fat",
            "Inode",
            "Small",
            "Medium",
            "Large",
        };
        printf("---------- %s ----------\n", name[i++]);
        printf("unit_cnt: %d\n",p->unit_cnt);
        printf("first_blk: %d\n",p->first_blk);
        printf("unit_size: %d\n",p->unit_size);
        printf("unit_free: %d\n",p->unit_free);
        printf("----------------------------\n");
    }

    uint32_t blk_cnt;
    uint32_t blk_size;
    uint32_t magic;
    printf("magic: 0x%x\n", super->magic);
    printf("blk_cnt: %d\n", super->blk_cnt);
    printf("blk_size: %d\n", super->blk_size);
    printf("-----------------------------\n");
    prt_part(&super->fat);
    prt_part(&super->inode);
    prt_part(&super->small);
    prt_part(&super->medium);
    prt_part(&super->large);
}

void super_test(void);
int main(void)
{
    super_test();
    return 0;
}

/*******************************************
 *  测试程序
********************************************/
void fill_test(void)
{
    struct moos_super_block a;
    a.blk_cnt = 12;
    a.small.unit_size = 1;
    a.medium.unit_size = 2;
    a.large.unit_size = 3;

    a.m_prop = 3;
    a.l_prop = 3;
    moos_fill_super(&a);
    prt_super(&a);
}

void super_test(void)
{
    fill_test();
}